<?php

namespace App\Exceptions;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Predis\Connection\ConnectionException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Throwable  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        if ($this->shouldntReport($exception)) {
            return;
        }
        Log::error("[ERROR_MESSAGE]",[
            'message' => $exception->getMessage(),
            'code' => $exception->getCode(),
            'file' => $exception->getFile(),
            'line' => $exception->getLine()]);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {

//        // 异常错误信息定义
//        $error_code = $exception->getCode() ?: 2;
//        $error_msg = $exception->getMessage();
//        Log::error("异常错误信息".$error_msg);
//        if ($exception instanceof QueryException)
//        {
//            $error_msg = "Db链接错误";
//        }
//        if ($exception instanceof ConnectionException)
//        {
//            $error_msg = "Cache链接错误";
//        }
//
//        if ($exception instanceof MethodNotAllowedHttpException)
//        {
//            $error_msg = "请求方式有误";
//        }
//
//        $err = ['error_code'=>$error_code,"error_msg" =>$error_msg,'data'=>'','sign'=>'','encryptKey'=>'','timestamp'=>time()];

//        return response()->json($err);
        return parent::render($request, $exception);

    }

}
